﻿package cn.poco.pepsi.Service_impl.impl;

import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import cn.poco.pepsi.Service_impl.ISearch;
import cn.poco.pepsi.bean.Area;
import cn.poco.pepsi.bean.Circle;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.bean.ResDetail;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.dao.CacheDbService;
import cn.poco.pepsi.util.UrlConnectionUtil;
import cn.poco.pepsi.util.xmlparse.AreaXmlparse;
import cn.poco.pepsi.util.xmlparse.CircleXmlparse;
import cn.poco.pepsi.util.xmlparse.DishXmlparse;
import cn.poco.pepsi.util.xmlparse.ResDetailXmlparse;
import cn.poco.pepsi.util.xmlparse.RestaurantXmlparse;


public class SearchImpl implements ISearch {

	@Override
	public List<Circle> getHotCircle(Long citycode, Integer num) throws Exception {
			String url = HOT_PATH.replace("#a", String.valueOf(citycode))
			.replace("#b", String.valueOf(num));
			InputStream inStream = UrlConnectionUtil.getRequest(url);
			Log.i(TAG, url);
			return CircleXmlparse.getXml(inStream);
	}

	@Override
	public ArrayList<Restaurant> getFilterRestaurant(String key, String lid,
			String dish, String cd, int s, String l, String dis,
			String vouch, String locationid, String dishid) throws Exception {
		InputStream inStream = null;
		StringBuffer sb = new StringBuffer();
			sb.append(BASE_SEARCH_PATH);
			if(key!=null){
				sb.append("?key=").append(URLEncoder.encode(key, "GBK"));
			}else{
				sb.append("?key=");
			}
			if(lid!=null && !lid.equals("")){
					sb.append("&lid=").append(lid);
				
			}
			if(dish!=null){
				sb.append("&dish=").append(dish);
			}
			if(cd!=null){
				sb.append("&cd=").append(cd);
			}
			if(String.valueOf(s)!=null){
				sb.append("&s=").append(s);
			}
			if(l!=null){
				sb.append("&l=").append(l);
			}
			if(dis!=null){
				sb.append("&dis=").append(dis);
			}
			if(vouch!=null){
				sb.append("&vouch=").append(vouch);
			}
			if(locationid!=null){
				sb.append("&locationid=").append(locationid);
			}
			if(dishid!=null){
				sb.append("&dishid=").append(dishid);
			}
			Log.i(TAG, sb.toString());
			inStream = UrlConnectionUtil.getRequest(sb.toString());
			
			return RestaurantXmlparse.getXml(inStream);
	}

	@Override
	public ResDetail getResDetail(String resid) throws Exception {
		String  reqUrl = null;
		InputStream instream = null;
		if(resid!="" && resid!=null){
			reqUrl = RESTAURANT_DEATAIL_PATH.replace("#o", resid);
			instream = UrlConnectionUtil.getRequest(reqUrl);
		}
		return ResDetailXmlparse.getXml(instream);
	}

	@Override
	public List<Dish> getDishs(final String citycode,String type,final Context context) throws Exception {
		String reqUrl = null;
		InputStream instream = null;
		List<Dish> dishs = null;
		if(citycode!="" && citycode!=null){
			reqUrl = DISH_PATH.replace("#a", citycode).replace("#b", type);
			instream = UrlConnectionUtil.getRequest(reqUrl);
			dishs = DishXmlparse.getXml(instream,citycode);
			Log.i(TAG, reqUrl);
			final List<Dish> threadDish = new ArrayList<Dish>();
			threadDish.addAll(dishs);
			new Thread(new Runnable() {
				@Override
				public void run() {
					//把菜式信息保存到数据库
					CacheDbService dishservice = new CacheDbService(context);
					int dishsize = 0;
					//判断数据库是否存在菜单记录
					dishsize = dishservice.checkDish(citycode);
					if(dishsize==0) dishservice.save((ArrayList<Dish>)threadDish);
					
				}
			}).start();
		}
		return dishs;
		
	}

	@Override
	public ArrayList<Area> getArea(String citycode) throws Exception {
		String url = null;
		InputStream instream = null;
		if(citycode!=null && !citycode.equals("")){
			url = AREA_PATH.replace("#a", citycode);
			instream = UrlConnectionUtil.getRequest(url);
		}
		return AreaXmlparse.getXml(instream);
	}

	@Override
	public ArrayList<Area> getSubArea(String citycode, String lid)
			throws Exception {
		String url = null;
		InputStream instream = null;
		if(citycode!=null && !citycode.equals("") 
				&& lid!=null && !lid.equals(""))
		{
			url = SUB_AREA_PATH.replace("#a", citycode)
						.replace("#b", lid);
			instream = UrlConnectionUtil.getRequest(url);
		}
		return AreaXmlparse.getXml(instream);
	}








}
